/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.ufcg.dsc.lsi.vademecum;

import br.edu.ufcg.dsc.lsi.vademecum.services.PresentationService;
import javax.microedition.lcdui.*;

/**
 * @author Hugo
 */
public class VadeMecumMIDPCanvas extends Canvas implements CommandListener {

    private Image map;
    private Command exitCommand;
    private Command zoomInCommand;
    private Command zoomOutCommand;
    private Command gpsCommand;
    private boolean linkGPS;
    private static VadeMecumMIDPCanvas instance = null;

    /**
     * constructor
     */
    private VadeMecumMIDPCanvas() {
        try {
            // Set up this canvas to listen to command events
            setCommandListener(this);
            linkGPS = false;
            exitCommand = new Command("Exit", Command.EXIT, 1);
            zoomInCommand = new Command("Zoom in", Command.SCREEN, 1);
            zoomOutCommand = new Command("Zoom out", Command.SCREEN, 1);
            gpsCommand = new Command("GPS", Command.SCREEN, 1);
            addCommand(exitCommand);
            addCommand(zoomInCommand);
            addCommand(zoomOutCommand);
            addCommand(gpsCommand);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static VadeMecumMIDPCanvas getInstance() {
        if (instance == null) {
            instance = new VadeMecumMIDPCanvas();
        }
        return instance;
    }

    /**
     * paint
     */
    public void paint(Graphics g) {
        g.drawImage(map, UP, UP, Graphics.TOP | Graphics.LEFT);
        drawIcons(g);
    }

    /**
     * Called when a key is pressed.
     */
    protected void keyPressed(int keyCode) {
    }

    /**
     * Called when a key is released.
     */
    protected void keyReleased(int keyCode) {
        System.out.println("código da chave: " + keyCode);
        PresentationService gMaps = PresentationService.getInstance();
        if (keyCode == KEY_NUM2 || keyCode == -1) {
            try {
                setMap(gMaps.panUp());
                repaint();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else if (keyCode == KEY_NUM4 || keyCode == -2) {
            try {
                setMap(gMaps.panLeft());
                repaint();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else if (keyCode == KEY_NUM6 || keyCode == -3) {
            try {
                setMap(gMaps.panRight());
                repaint();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else if (keyCode == KEY_NUM8 || keyCode == -4) {
            try {
                setMap(gMaps.panDown());
                repaint();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else if (keyCode == KEY_NUM3) {
            try {
                setMap(gMaps.zoomIn());
                repaint();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else if (keyCode == KEY_NUM9) {
            try {
                setMap(gMaps.zoomOut());
                repaint();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * Called when a key is repeated (held down).
     */
    protected void keyRepeated(int keyCode) {
    }

    /**
     * Called when the pointer is dragged.
     */
    protected void pointerDragged(int x, int y) {
    }

    /**
     * Called when the pointer is pressed.
     */
    protected void pointerPressed(int x, int y) {
    }

    /**
     * Called when the pointer is released.
     */
    protected void pointerReleased(int x, int y) {
    }

    /**
     * Called when action should be handled
     */
    public void commandAction(Command command, Displayable displayable) {
        PresentationService gMaps = PresentationService.getInstance();
        if (command == zoomInCommand) {
            try {
                setMap(gMaps.zoomIn());
                repaint();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else if (command == zoomOutCommand) {
            try {
                setMap(gMaps.zoomOut());
                repaint();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else if (command == gpsCommand) {
            try {

                linkGPS = !linkGPS;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public Image getMap() {
        return map;
    }

    public void setMap(Image map) {
        this.map = map;
    }

    private void drawIcons(Graphics g) {
        PresentationService gMaps = PresentationService.getInstance();
        int[] pixels = gMaps.coordToPixel(-7.0, -35.0);
        g.drawString("João Pessoa", pixels[0], pixels[1], Graphics.TOP | Graphics.LEFT);
    }
}